package com.atf.atfconfigurator.views;

import java.util.Iterator;

import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration;

import com.atf.atfconfigurator.AtfconfiguratorUI;
import com.atf.atfconfigurator.composite.CollapseableBox;
import com.atf.atfconfigurator.utils.WasConstant;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class MQQueueView extends VerticalLayout implements View{
	private PropertiesConfiguration wasConfigProperty;
	@Override
	public void enter(ViewChangeEvent event) {
		// TODO Auto-generated method stub
		wasConfigProperty=((AtfconfiguratorUI) getUI()).getWasConfiguration();	
		setSpacing(true);
        setMargin(true);

        Label title = new Label(wasConfigProperty.getString("appName"));
        title.addStyleName("h2");
        title.addStyleName("colored");
        addComponent(title);
        
        final FormLayout form = new FormLayout();
        form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        addComponent(form);
        
        final TextField noOfQDs = new TextField("Number of QD");
	     int noOfQDsValue=wasConfigProperty.getInt("noOfQDs",0);
	     noOfQDs.setValue(String.valueOf(noOfQDsValue));
	     noOfQDs.setWidth("50%");
	     form.addComponent(noOfQDs);
	     
	     noOfQDs.addValueChangeListener(new Property.ValueChangeListener(){

				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				@Override
				public void valueChange(ValueChangeEvent event) {
					// TODO Auto-generated method stub
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("noOfQDs",  (String) event.getProperty().getValue());
					((AtfconfiguratorUI) getUI()).navigator.navigateTo("MQQueue");
				}        	
	        });
	     if(noOfQDsValue > 0){
	    	 for(int i=1; i<noOfQDsValue+1; i++){
	    		 form.addComponent(new CollapseableBox(String.valueOf(i),getOfQDsEntries(i), false));
	    	 }
	     }
	}
	private Component getOfQDsEntries(final int pos) {
		// TODO Auto-generated method stub
		final FormLayout form = new FormLayout();
		//application Name
		form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light"); 
        HierarchicalConfiguration sub = WasConstant.getWasConstantFile().configurationAt("queueDestination");
        Iterator iterator=sub.getKeys();
        while(iterator.hasNext()){
        	String key=(String) iterator.next();
        	String value=sub.getString(key);
    		if( wasConfigProperty.containsKey("qd"+pos+key)){
    			value=wasConfigProperty.getString("qd"+pos+key);
    		}else{
    			((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("qd"+pos+key,value);
    		}
    		
	    		final TextField parNameText = new TextField(key);
	    		parNameText.setValue(value);
	    		parNameText.setWidth("100%");
	    		parNameText.addValueChangeListener(new Property.ValueChangeListener(){
	
					@Override
					public void valueChange(ValueChangeEvent event) {
						// TODO Auto-generated method stub
						String value = (String) event.getProperty().getValue();
						((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("qd"+pos+parNameText.getCaption(), value);
					}
	            	
	            });
	    		parNameText.setImmediate(true);
	            form.addComponent(parNameText);
    		
        }
		return form;
	}
	
	
}
